package com.wanjian.sak.layer.impl;

import android.graphics.Canvas;
import android.graphics.Region;
import android.view.View;
import android.view.ViewGroup;

import com.wanjian.sak.layer.Layer;

public abstract class ViewLayer extends Layer   {
  private int mStartLayer;
  private int mEndLayer = 100;


  @Override
  protected void onAttach(View rootView) {
    super.onAttach(rootView);
  }

  @Override
  protected final void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    traversals(canvas, getRootView(), 0);
  }

  @Override
  protected void onAfterTraversal(View rootView) {
    super.onAfterTraversal(rootView);
    invalidate();
  }



  private void traversals(Canvas canvas, View view, int curLayer) {
    if (curLayer > mEndLayer) {
      return;
    }
    if (curLayer >= mStartLayer) {
      int count = canvas.save();
      onDraw(canvas, view);
      canvas.restoreToCount(count);
    }

    if (view instanceof ViewGroup) {
      ViewGroup vg = ((ViewGroup) view);
      int sx = view.getScrollX();
      int sy = view.getScrollY();
      canvas.save(); //save 1
      canvas.translate(-sx, -sy);

      for (int i = 0, len = vg.getChildCount(); i < len; i++) {
        View child = vg.getChildAt(i);
        canvas.save();//save 2
        canvas.translate(child.getX(), child.getY());
        traversals(canvas, child, curLayer + 1);
        canvas.restore();// restore 2
      }
      canvas.restore(); // restore 1
    }
  }

  protected abstract void onDraw(Canvas canvas, View view);

}
